module top(
           input [31: 0] a,
           input [31: 0] b,
           input clk,
           input rst_n,

           output reg [31: 0] yshang,
           output reg [31: 0] yyushu
       );
// 组合逻辑
reg [63: 0] temp_a;
reg [63: 0] temp_b;
reg [31: 0] tempa;
reg [31: 0] tempb;
always @(a or b)
	begin
		tempa = a;
		tempb = b;
	end
// assign temp_b = {tempb, 32'd0};
// assign temp_a = {32'd0, tempa};

integer i;
always @(tempa or tempb)
	begin
		temp_b = {tempb, 32'd0};
		temp_a = {32'd0, tempa};
		for (i = 0;i < 32;i = i + 1)
			begin
				temp_a = {temp_a[62: 0], 1'b0};
				if (temp_a[63: 32] >= tempb)
					temp_a = temp_a - temp_b + 1'b1;
				else
					temp_a = temp_a;
			end
		yshang = temp_a[31: 0];
		yyushu = temp_a[63: 32];
	end


//时序逻辑
always @(*)
	if (!rst_n)
		begin
			tempa = 0;
			tempb = 0;
		end
	else
		begin
			tempa = a;
			tempb = b;
		end
parameter idle = 2'b00;
parameter s0 = 2'b01;
parameter s1 = 2'b10;
parameter s2 = 2'b11;
reg [5: 0] cnt;

reg [1: 0] state, state_next;

always@(posedge clk or negedge rst_n)
	begin
		if (!rst_n)
			state <= idle;
		else
			state <= state_next;
	end

always@( * )
	begin
		if (!rst_n)
			begin
				state_next = idle;
				cnt = 6'd0;
			end
		else
			begin
				case (state)
					idle:
						begin
							state_next = s0;
							cnt = 0;
							temp_b = {tempb, {32{1'b0}}};
							temp_a = {{32{1'b0}}, tempa};
						end
					s0:
						begin
							cnt = cnt + 1'b1;
							temp_a = {temp_a[62: 0], 1'b0};
							state_next = s1;
						end
					s1:
						begin
							if (temp_a[63: 32] >= tempb)
								temp_a = temp_a - temp_b + 1'b1;
							else
								temp_a = temp_a;
							if (cnt < 6'd32)
								state_next = s0;
							else
								state_next = s2;
						end
					s2:
						begin
							state_next = idle;
						end
				endcase
			end
	end

always@(posedge clk or negedge rst_n)
	begin
		if (!rst_n)
			begin
				yshang <= 0;
				yyushu <= 0;
			end
		else if (state == s2)
			begin
				yshang <= temp_a[31: 0];
				yyushu <= temp_a[63: 32];
			end
	end
endmodule
